package leetcode;

public class RemoveDuplicatesPlus82 {
    // 递归求解
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        if (head.val == head.next.val) {
            ListNode pointer = head.next;
            while (pointer.next != null && pointer.val == pointer.next.val) { // pointer.next不能为null 这里也容易忽略
                pointer = pointer.next;
            }
            return deleteDuplicates(pointer.next); // 这里不应该直接返回pointer.next 因为不确定 pointer.next是不是重复
        } else {
            head.next = deleteDuplicates(head.next); // 是下一节点 这里容易忽略
            return head;
        }
    }

    // 指针解题
    public static ListNode deleteDuplicates2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode s = new ListNode(-1, head);
        ListNode p1 = s;
        ListNode p2, p3;
        while ((p2 = p1.next) != null && (p3 = p2.next) != null) {
            if (p2.val == p3.val) {
                while ((p3 = p3.next) != null && p3.val == p2.val) {
                }
                p1.next = p3;
            } else {
                p1 = p1.next;
            }
        }
        return s.next;
    }

    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}

